﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Configuration
{
    public class SerialPortConfig
    {
        static String port = null;
        static int baudRate = -1;
        static int parity = -1;
        static int stopBits =-1;
        static ushort dataBits = 8;
        const String sectionName = "SerialPort";

        public static Boolean HasSection
        {
            get
            {
                return InitConfig.hasSection(sectionName);
            }
        }

        public static string Port {
            get
            {

                if (port == null)
                {
                    port = InitConfig.getString(sectionName, "portName", null);
                }
                return port;
            }
        }
        public static int BaudRate {
            get
            {

                if (baudRate == -1)
                {
                    String str = InitConfig.getString(sectionName, "baudRate", null);
                    baudRate =  Int32.Parse(str);
                }
                return baudRate;
            }
        }
        public static Parity Parity
        {
            get
            {

                if (parity == -1)
                {
                    String str = InitConfig.getString(sectionName, "parity", null);
                    parity = Int32.Parse(str);
                }
                return (Parity)parity;
            }
            //return ConfigurationManager.AppSettings["DataSourceString"];
        }

        public static ushort DataBits
        {
            get
            {

                if (dataBits == 0xff)
                {
                    String str = InitConfig.getString(sectionName, "dataBits", null);
                    dataBits = ushort.Parse(str);
                }
                return dataBits;
            }
        }
        public static StopBits StopBits
        {
            get
            {
                //StopBits _stopBits = StopBits.None;

                if (stopBits == -1)
                {
                    String str = InitConfig.getString(sectionName, "stopBits", null);
                    stopBits = Int32.Parse(str);
                }
                return (StopBits)stopBits;
            }
        }
    }
}
